package flextv.main.logic.download {

import flash.filesystem.File;

import flextv.core.download.IDownloadable;
import flextv.core.logic.Logger;

import mx.logging.ILogger;

public class DownloadServiceImpl implements IDownloadService {
	
	private static const LOG: ILogger = Logger.getLog(DownloadServiceImpl);
	
	public function DownloadServiceImpl() {
	}
	
	public function download(item: IDownloadable, target: File, fnFlvUriReady: Function): void {
		LOG.info("download(item="+item+"; target="+target.nativePath+")");
		
		new DownloadDelegate(item, target, fnFlvUriReady).download();
	}
	
	
	/*
	private var stream: URLStream;
	public function download(item: IDownloadable, target: File): void {
		LOG.info("download(item="+item+"; target="+target.nativePath+")");
		
		new Swf2FlvConverter(item.swfUrl, function(flvUrl: String): void {
		
			const request: URLRequest = new URLRequest(flvUrl);
			stream = new URLStream();
			stream.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onResponseStatus);
			
			stream.addEventListener(ProgressEvent.PROGRESS, function(event: ProgressEvent):void {
				const percent: int = event.bytesLoaded / event.bytesTotal * 100;
				// LOG.finest("onProgress(event="+event+") percent=" + percent);
				item.downloaded = percent;
			});
			
			stream.addEventListener(Event.COMPLETE, function(event: Event):void {
				const fileData: ByteArray = new ByteArray();
				stream.readBytes(fileData, 0, stream.bytesAvailable);
				
				LOG.fine("Saving downloaded FLV-file to [" + target.nativePath + "]");
				
				var fileStream: FileStream = new FileStream();
				fileStream.open(target, FileMode.WRITE);
				fileStream.writeBytes(fileData, 0, fileData.length);
				fileStream.close();
			});
			
			LOG.finer("Loading stream request for url ["+request.url+"].");
			stream.load(request);
			
		}).doConvert();
	}
	
	private function onResponseStatus(event: HTTPStatusEvent): void {
		if(event.status == 404) {
			Alert.show("Not Found: " + event.responseURL, "404 ERROR");
		}
	}
	*/	
}
}